<template>
  <div class="page-root">
    <BasePanel>
      <BaseSearch :search="searchProps" @searchData="updateTable" @resetData="updateTable" />
      <div class="table-top-wrap">
        <div class="table-title">
          <span>列表</span>
        </div>
      </div>
      <BaseTable :loading="loading" :columns="columns" :dataSource="dataSource" :pagination="false"
        :scroll="{ x: 1000 }">
        <template slot="loanMappingVOList" slot-scope="{ record }">
          <template v-if="record.loanMappingVOList && record.loanMappingVOList.length">
            <a-tag color="blue" :key="item.loanId" v-for="item in record.loanMappingVOList">{{ item.productName
              }}</a-tag>
          </template>
          <span v-else>/</span>
        </template>
        <template slot="operation" slot-scope="{ record }">
          <span class="primary" @click="handleEdit(record)">编辑</span>
          <a-divider type="vertical" />
          <span class="primary" @click="handleSynchronize(record)">同步</span>
        </template>
      </BaseTable>
      <div v-show="dataSource.length > 0" class="page-footer">
        <BasePagination :page-key.sync="pageKey" :get-list="tableInit" />
      </div>
    </BasePanel>
    <SortModal :visible="sortShow" :id="id" @success="tableInit" @close="sortShow = false" />
    <SynchronizeModal :visible="synchronizeShow" :default="defaultObj" @success="successForm"
      @close="closeSynchronize" />
  </div>
</template>

<script>
import { searchProps, columns } from './option.js';
import * as Ajax from '@/api/loan';
import SortModal from './sort';
import SynchronizeModal from './synchronize.vue';
export default {
  components: {
    SortModal,
    SynchronizeModal,
  },
  data() {
    return {
      searchProps,
      searchForm: {},
      columns,
      loading: true,
      dataSource: [],
      pageKey: {
        pageSize: 10,
        total: 1,
        current: 1,
      },
      id: '',
      sortShow: false,
      defaultObj: {},
      synchronizeShow: false,
    };
  },
  created() {
    this.tableInit();
    this.getSelect();
  },
  methods: {
    getSelect() {
      const params = { type: 1 };
      Ajax.getChannelSelect(params).then((res) => {
        if (res.success) {
          searchProps[0].selectOptions = Array.isArray(res.data)
            ? res.data.map((it) => ({
              ...it,
              label: it.channelName,
              value: it.id,
            }))
            : [];
        }
      });
    },
    updateTable(params = {}) {
      this.searchForm = { ...params };
      this.pageKey = this.$options.data().pageKey;
      this.tableInit();
    },
    // table 初始化
    tableInit() {
      this.loading = true;
      const { current, pageSize } = this.pageKey;
      const params = { pageNum: current, pageSize, ...this.searchForm };
      Ajax.getMappingList(params)
        .then((res) => {
          const { data, totalCount } = res;
          this.dataSource = Array.isArray(data) ? data : [];
          this.pageKey.total = totalCount;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 编辑
    handleEdit(record) {
      const { channelId } = record;
      this.id = channelId;
      this.sortShow = true;
    },
    handleSynchronize(record) {
      const { channelId, channelName } = record;
      this.defaultObj = {
        channelId,
        channelName,
      };
      this.synchronizeShow = true;
    },
    successForm() {
      this.tableInit();
      this.closeForm();
    },
    closeForm() {
      this.formShow = false;
      this.defaultObj = {};
    },
    closeSynchronize() {
      this.synchronizeShow = false;
      this.defaultObj = {};
    },
  },
};
</script>

<style lang="scss" scoped>
.table-wrap {
  margin-top: 16px;
}

.table-top-wrap {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.table-title {
  flex: 1;

  span {
    line-height: 32px;
    font-weight: bold;
    font-size: 16px;
    display: flex;
    align-items: center;

    &::before {
      content: '';
      display: block;
      width: 3px;
      height: 18px;
      background-color: #1890ff;
      margin-right: 8px;
      position: relative;
      top: -1px;
    }
  }
}

.btn-gruop {
  display: flex;
  justify-content: flex-end;

  &>button {
    margin-left: 12px;

    &:first-child {
      margin-left: 0;
    }
  }
}
</style>
